unit UFormPrincipal;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, Grids, UCelula, UPlanilha, ComCtrls;

type
  TForm1 = class(TForm)
    BtnSalvar: TBitBtn;
    BtnCarregar: TBitBtn;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    StGridDados: TStringGrid;
    StGridResultados: TStringGrid;
    Label4: TLabel;
    Label5: TLabel;
    Edit2: TEdit;
    Edit3: TEdit;
    BtnPlanilha: TButton;
    BtnTransf: TButton;
    BtnCalcular: TBitBtn;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure BtnSalvarClick(Sender: TObject);
    procedure BtnCarregarClick(Sender: TObject);
    procedure BtnPlanilhaClick(Sender: TObject);
    procedure BtnTransfClick(Sender: TObject);
    procedure BtnCalcularClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Celula1 : TCelula;
  Lista : TList;
  Planilha1 : TPlanilha;

implementation



{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Celula1 := TCelula.Create;
  Lista := TList.Create;
  Planilha1 := TPlanilha.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Celula1.Free;
  Lista.Free;
  Planilha1.Free;
end;

procedure TForm1.BtnSalvarClick(Sender: TObject);
  var  St : TMemoryStream;
begin
//  St := TMemoryStream.Create;
//  Celula1.GetStream(St);
//  St.Position := 0;
//  St.SaveToFile('teste.str');
  St := TMemoryStream.Create;
  Planilha1.GetStream(St);
  St.Position := 0;
  St.SaveToFile('planilha.str');
  St.Free;
end;

procedure TForm1.BtnCarregarClick(Sender: TObject);
  var  St : TMemoryStream;
       x : real;
begin
//  St := TMemoryStream.Create;
//  St.LoadFromFile('teste.str');
//  Celula1.SetStream(St);
//  Label2.Caption := 'String: '+Celula1.GetValor;
//  if Celula1.GetFloat(x)then
//  Label3.Caption := 'Float: '+FloatToStr(x);
  St := TMemoryStream.Create;
  St.LoadFromFile('planilha.str');
  Planilha1.SetStream(St);
  St.Free;
  Planilha1.RetornaGridDados(StGridDados);
  Edit3.Text := IntToStr(StGridDados.RowCount-1);
  Edit2.Text := IntToStr(StGridDados.ColCount-1);
end;

procedure TForm1.BtnPlanilhaClick(Sender: TObject);
begin
  StGridDados.RowCount := StrToInt(Edit3.Text)+1;
  StGridDados.ColCount := StrToInt(Edit2.Text)+1;
  StGridDados.Visible := true;
  StGridDados.Cells[0,0] := 'Lin\Col';
end;

procedure TForm1.BtnTransfClick(Sender: TObject);
  var Nlin, Ncol, i,j : word;
      st : string;
begin
  Nlin := StGridDados.RowCount-1; Ncol := StGridDados.ColCount-1;
  Planilha1.SetNLinsCols(Nlin,Ncol);
  for i := 1 to Ncol do Planilha1.TituloColunas[i] := StGridDados.Cells[i,0];
  for i := 1 to Nlin do Planilha1.TituloLinhas[i] := StGridDados.Cells[0,i];
  for i := 1 to Nlin do
   for j := 1 to Ncol do
     begin
       st := StGridDados.Cells[j,i];
       if copy(st,1,1) = '=' then
         begin
           Planilha1.Linhas[i].Colunas[j].SetTipo('formula');;
           Planilha1.Linhas[i].Colunas[j].SetFormula(st);
         end
       else
         begin
           Planilha1.Linhas[i].Colunas[j].SetTipo('valor');;
           Planilha1.Linhas[i].Colunas[j].SetValor(st);
         end;
     end;

end;

procedure TForm1.BtnCalcularClick(Sender: TObject);
begin
  Planilha1.Calcular;
  Planilha1.RetornaGridResultados(StGridResultados);
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  StGridDados.Visible := true;
  StGridDados.Cells[0,0] := 'Lin\Col';
  BtnCarregarClick(Sender);
  BtnTransfClick(Sender);  
end;

end.
